#!/bin/sh
#
# Perform necessary datadog-dogstatsd setup steps after package is installed.
#
# .deb: STEP 5 of 5

INSTALL_DIR=/opt/datadog-dogstatsd
LOG_DIR=/var/log/datadog
CONFIG_DIR=/etc/datadog-dogstatsd
SERVICE_NAME=datadog-dogstatsd

# If we are inside the Docker container, do nothing
if [ -n "$DOCKER_DD_AGENT" ]; then
    echo "Installation from docker-dd-agent, nothing to do in postinst"
    exit 0
fi

set -e
case "$1" in
    configure)
        # Only create dd-agent group and/or user if they don't already exist
        getent group dd-agent >/dev/null || (echo "Creating dd-agent group" && addgroup --system dd-agent --quiet)
        set +e
        id -u dd-agent >/dev/null 2>&1
        USER_EXISTS=$?
        set -e
        if [ ! $USER_EXISTS -eq 0 ]; then
            echo "Creating dd-agent user"
            adduser --system dd-agent --disabled-login --shell /usr/sbin/nologin --home ${INSTALL_DIR} --no-create-home --group --quiet
        elif id -nG dd-agent | grep --invert-match --word-regexp --quiet 'dd-agent'; then
            # User exists but is not part of the dd-agent group
            echo "Adding dd-agent user to dd-agent group"
            usermod -g dd-agent dd-agent
        fi

        # Create a symlink to the agent's binary
        ln -sf $INSTALL_DIR/bin/dogstatsd /usr/bin/datadog-dogstatsd
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
    ;;
esac

# Set proper rights to the dd-agent user
chown -R dd-agent:dd-agent ${CONFIG_DIR}
chown -R dd-agent:dd-agent ${LOG_DIR}
chown -R dd-agent:dd-agent ${INSTALL_DIR}

# Enable and restart the dogstatsd service here on Debian platforms
# On RHEL, this is done in the posttrans script
# Only supports systemd and upstart
echo "Enabling service $SERVICE_NAME"
if command -v systemctl >/dev/null 2>&1; then
    systemctl enable $SERVICE_NAME || echo "[ WARNING ]\tCannot enable $SERVICE_NAME with systemctl"
elif command -v initctl >/dev/null 2>&1; then
    # Nothing to do, this is defined directly in the upstart job file
    :
else
    echo "[ WARNING ]\tCannot detect a supported init system. The datadog-dogstatsd package only provides service files for systemd and upstart."
fi

# TODO: Use a configcheck command on the agent to determine if it's safe to restart,
# and avoid restarting when a check conf is invalid
if [ -f "$CONFIG_DIR/datadog.yaml" ]; then
    echo "(Re)starting $SERVICE_NAME now..."
    if command -v systemctl >/dev/null 2>&1; then
        systemctl restart $SERVICE_NAME || true
    elif command -v initctl >/dev/null 2>&1; then
        initctl start $SERVICE_NAME || initctl restart $SERVICE_NAME || true
    else
        echo "[ WARNING ]\tCannot detect a supported init system. The datadog-dogstatsd package only provides service files for systemd and upstart."
    fi
else
    # No dogstatsd.yaml file is present. This is probably a clean install made with the
    # step-by-step instructions/an automation tool, and the config file will be added next.
    echo "No dogstatsd.yaml file detected, not starting dogstatsd"
fi

exit 0
